package org.fas.extral
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class FeAS3Crypto
	{
		static public var keyString:String = 'test';
		static public var type:String='simple-des-ecb';
	 	static public function encrypt(txt:String,keyString:String='',type:String=''):String
        {
        	var key:ByteArray;
        	keyString=keyString==''?FeAS3Crypto.keyString:keyString;
        	type = type==''?FeAS3Crypto.type:type;
        	key = Hex.toArray(Hex.fromString(keyString));// can only be 8 characters long
            var data:ByteArray = Hex.toArray(Hex.fromString(txt));
            var pad:IPad = new PKCS5;
            var mode:ICipher = Crypto.getCipher(type, key, pad);
            pad.setBlockSize(mode.getBlockSize());
            mode.encrypt(data);
            return Base64.encodeByteArray(data);
        }
		static public function decrypt(txt:String,keyString:String='',type:String=''):String
		{
			var key:ByteArray;
			keyString=keyString==''?FeAS3Crypto.keyString:keyString;
			type = type==''?FeAS3Crypto.type:type;
			key = Hex.toArray(Hex.fromString(keyString));// can only be 8 characters long
			var data:ByteArray = Base64.decodeToByteArray(txt);
			var pad:IPad = new PKCS5;
			var mode:ICipher = Crypto.getCipher(type, key, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.decrypt(data);
			return Hex.toString(Hex.fromArray(data));
		}
	}
}